package com.ebt.m.proposal_v2.utils;

import android.text.TextUtils;
import android.util.Log;

import com.ebt.m.AppContext;
import com.ebt.m.proposal_v2.bean.DateTime;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * @author archer.qi
 *         Created on 2017/5/9.
 */
public class DateUtils {

    /**
     * 从Calendar解析出日期字段
     *
     * @param c
     * @return
     */
    public static DateTime parseCalendar(Calendar c) {
        DateTime dateTime = new DateTime();
        dateTime.year = c.get(Calendar.YEAR);
        dateTime.month = c.get(Calendar.MONTH);
        dateTime.day = c.get(Calendar.DATE);
        dateTime.hour = c.get(Calendar.HOUR_OF_DAY);
        dateTime.minute = c.get(Calendar.MINUTE);
        dateTime.second = c.get(Calendar.SECOND);
        return dateTime;
    }

    public static String formatDate(Date date) {
        if (date == null) {
            return null;
        }
        return new SimpleDateFormat("yyyy-MM-dd").format(date);
    }

    public static DateTime toDateTime(Date date) {
        if (date == null) {
            return null;
        }
        Calendar c = Calendar.getInstance();
        c.setTime(date);
        return parseCalendar(c);
    }

    public static DateTime toDateTime(long millis) {
        Calendar c = Calendar.getInstance();
        c.setTimeInMillis(millis);
        return parseCalendar(c);
    }

    public static Date toDate(int year, int month, int day) {
        Calendar c = Calendar.getInstance();
        c.set(year, month, day);
        return c.getTime();
    }

    public static Date toDate(String dateTime, String regex) {
        if (TextUtils.isEmpty(dateTime)) {
            return null;
        }
        SimpleDateFormat dateFormat = new SimpleDateFormat(regex);
        Date target = null;
        try {
            target = dateFormat.parse(dateTime);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return target;
    }

    public static String toDateString(long millis, String regex) {
        return new SimpleDateFormat(regex).format(new Date(millis));
    }

    public static int thisYear() {
        return Calendar.getInstance().get(Calendar.YEAR);
    }

    public static int thisMonth() {
        return Calendar.getInstance().get(Calendar.MONTH);
    }

    public static int today() {
        return Calendar.getInstance().get(Calendar.DATE);
    }

    /**
     * 根据指定生日获取到今天的日期差，单位：天
     *
     * @param date
     * @return
     */
    public static int getDayDiffByNow(Date date) {
        long currentMillis = System.currentTimeMillis();
        long birthdayMillis = date.getTime();
        long millisDiff = currentMillis - birthdayMillis;
        return (int) (millisDiff / (1000 * 60 * 60 * 24));
    }

    /**
     * 根据生日算年龄
     * <p>
     * 如果还没过生日，则年龄 = 按年份算的年龄 - 1；
     *
     * @param year
     * @param month
     * @param day
     * @return
     */
    public static int getCalculatedAgeByBirthday(int year, int month, int day) {
        int generalizedAge = thisYear() - year; // 广义上的年龄
        if (thisMonth() > month) {
            return generalizedAge;
        }
        if (thisMonth() < month) {
            return generalizedAge - 1;
        }
        if (thisMonth() == month) {
            if (today() >= day) {
                return generalizedAge;
            } else {
                return generalizedAge - 1;
            }
        }
        return generalizedAge;
    }

    /**
     * 根据生日算年龄，没过生日不算一岁
     *
     * @param birthday
     * @return
     */
    public static int getAgeByActualBirthday(Date birthday) {
        Calendar c = Calendar.getInstance();
        c.setTime(birthday);

        int year = c.get(Calendar.YEAR);
        int month = c.get(Calendar.MONTH) - 1;
        int day = c.get(Calendar.DAY_OF_MONTH);
        return getCalculatedAgeByBirthday(year, month, day);
    }

    public static int getAgeByActualBirthdayWithMonthOriginal(Date birthday) {
        Calendar c = Calendar.getInstance();
        c.setTime(birthday);

        int year = c.get(Calendar.YEAR);
        int month = c.get(Calendar.MONTH);
        int day = c.get(Calendar.DAY_OF_MONTH);
        return getCalculatedAgeByBirthday(year, month, day);
    }

    /**
     * 是否超过今日
     *
     * @param year
     * @param month
     * @param day
     * @return
     */
    public static boolean isDateOutpace(int year, int month, int day) {
        long currentMillis = AppContext.getCurrentTime();
        Calendar calendar = Calendar.getInstance();
        calendar.set(year, month, day);
        long millis = calendar.getTimeInMillis();
        if (millis > currentMillis) {
            return true;
        }
        return false;
    }

    /**
     * 根据年龄初始化生日， 默认为指定年的1月1日
     *
     * @param age
     * @param split
     * @return
     */
    public static String getBirthdayByAge(int age, String split) {
        if (age < 0) {
            return "";
        }
        int thisYear = thisYear();
        int year = thisYear - age;
        return year + split + "01" + split + "01";
    }

    /**
     * 判断当前时间是否处于指定时间范围内
     *
     * @param startTime
     * @param endTime
     * @return
     */
    public static boolean isBetween(String startTime, String endTime) {
        if (TextUtils.isEmpty(startTime) || TextUtils.isEmpty(endTime)) {
            return false;
        }
        SimpleDateFormat format = new SimpleDateFormat("yyyy/MM/dd hh:mm:ss");
        try {
            long start = format.parse(startTime).getTime();
            long end = format.parse(endTime).getTime();
            long current = AppContext.getCurrentTime();

            return current >= start && current <= end;
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return false;
    }

}
